package socket;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class ServerSuper {
    public static void main(String[] args) throws IOException {
            //创建一个集合,记录socket
            Vector<Socket> sockets = new Vector<>();
            //创建服务端套接字
            ServerSocket ss = new ServerSocket(11111);
            //创建线程池
            ExecutorService es = Executors.newFixedThreadPool(100);
            //绑定套接字
            for (int i = 0; i < 10; i++) {
                es.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {

                            Socket accept = ss.accept();

                            //添加集合
                            sockets.add(accept);

                            InetAddress inetAddress = accept.getInetAddress();

                            String hostName = inetAddress.getHostName();


                            InputStream is=accept.getInputStream();


                            byte[] bytes = new byte[1024];


                            int len;

                            while (true) {

                                if (accept.isClosed()){
                                    System.out.println(hostName+"断开连接了");
                                    break;
                                }


                                len= is.read(bytes);
                                //客户端将用进行的操作
                                String result = new String(bytes, 0, len);

                                if (result.equals("1")) {

                                    while ((len = is.read(bytes)) != -1) {
                                        System.out.println("-----当前用户" + hostName + "----");
                                        System.out.println(hostName + "系统:接收到客户端消息" + new Date());
                                        System.out.print("客户端消息 : "+new String(bytes, 0, len));
                                        //返回信息
                                        System.out.println(hostName + "系统:请返回消息给客户端" + new Date());
                                        InputStream in = System.in;
                                        byte[] bytes1 = new byte[1024];
                                        int len1;
                                        len1 = in.read(bytes1);
                                        accept.getOutputStream().write(bytes1, 0, len);
                                        System.out.println(hostName + "系统:成功发送消息" + new Date());
                                        System.out.println("结束-----"+hostName + "---------");
                                        System.out.println();
                                        break;
                                    }
                                }

                                if (result.equals("2")) {
                                    System.out.println(hostName+"请求传送文件");
                                    //写文件

                                    bytes = new byte[1024];


                                    FileOutputStream fos = new FileOutputStream( "C:\\Users\\Administrator\\Desktop\\"+UUID.randomUUID() + ".txt");

                                    //todo
                                    while ((len = is.read(bytes)) != -1) {

                                        fos.write(bytes,0,len);
                                        //少于1024说明读完了,不用在等客户端传送数据了
                                        if (len<1024){
                                            break;
                                        }

                                    }
                                    System.out.println(hostName+"文件上传成功!");
                                    accept.getOutputStream().write((hostName+"上传成功!").getBytes());

                                    //关流
                                    fos.close();
                                }

                                if (result.equals("3")) {
                                    System.out.println(hostName+"断开了连接!");
                                    accept.close();
                                    break;
                                }

                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }

            //在用一个线程,记录当前服务器连接的客户端数量
            es.submit(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        //每隔一分钟获取一次结果
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        getClints(sockets);
                    }
                }
            });

        }

        //todo  明明已经关闭了客户互动,为社么还是在线状态
        //获取当前服务端连接的客户端数量
        public static void getClints(Vector<Socket> vector){


            AtomicInteger count=new AtomicInteger();
            //遍历集合
            for (Socket socket : vector) {
                if (!socket.isClosed()){
                    System.out.println(socket.getInetAddress().getHostName()+" 状态 "+(!socket.isClosed())+"  时间 "+ LocalDateTime.now());
                    count.incrementAndGet();
                }
            }

            System.out.println("一分钟刷新一次  当前在线用户数量 : "+count);
        }
    }

